package com.zjs.servlet;

import com.zjs.bean.Grade;
import com.zjs.bean.Student;
import com.zjs.service.GradeService;
import com.zjs.service.StudentService;
import com.zjs.service.impl.GradeServiceImpl;
import com.zjs.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/Educational/student"})
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式（post）
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("list".equals(method)) {
            this.list(req, resp);
        } else if ("add".equals(method)) {
            this.add(req, resp);
        } else if ("update".equals(method)) {
            this.update(req, resp);
        } else if ("delete".equals(method)) {
            this.delete(req, resp);
        } else if ("findById".equals(method)) {
            this.findById(req, resp);
        }
    }

    /**
     * 根据id查询学生
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        //查询学生信息
        Student student = studentService.findById(stuId);
        //获取班级数据
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> grades = gradeService.list();

        req.setAttribute("student", student);
        req.setAttribute("grades", grades);
        //跳转到编辑页面
        req.getRequestDispatcher("/Educational/student/edit.jsp").forward(req, resp);
    }

    /**
     * 删除学生
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String stuId = req.getParameter("stuId");
        //删除
        int i = studentService.delete(stuId);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.write("<script>alert('删除成功！');location.href='/Educational/student?method=list'</script>");
        } else {
            //删除失败
            writer.write("<script>alert('删除失败!');location.href='/Educational/student?method=list'</script>");
        }
    }

    /**
     * 更新学生
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuId = req.getParameter("stuId");
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        Student student = new Student(stuNo, stuName, Integer.parseInt(gid), Integer.parseInt(sex), email, phone, registered, address, politics, idNumber, profession, introduction);
        student.setStuId(Integer.parseInt(stuId));
        //储存数据
        int i = studentService.update(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.write("<script>alert('修改成功！');location.href='/Educational/student?method=list'</script>");
        } else {
            //删除失败
            writer.write("<script>alert('修改失败!');location.href='/Educational/student?method=findById&stuId=" + stuId + "'</script>");
        }
    }

    /**
     * 添加学生
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        Student student = new Student(stuNo, stuName, Integer.parseInt(gid), Integer.parseInt(sex), email, phone, registered, address, politics, idNumber, profession, introduction);
        //储存数据
        int i = studentService.add(student);
        //跳转页面
        if (i > 0) {
            //跳转到列表页面
            resp.sendRedirect("/Educational/student?method=list");
        } else {
            resp.sendRedirect("/grade");
        }

    }

    /**
     * 查询学生列表
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        //获取学生名称、学生学号、性别、获取页码、获取每页显示条数
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");

        Integer sex = req.getParameter("sex") != null ? Integer.parseInt(req.getParameter("sex")) : -1;
        Integer pageIndex = req.getParameter("pageIndex") != null ? Integer.parseInt(req.getParameter("pageIndex")) : 1;
        Integer pageSize = req.getParameter("pageSize") != null ? Integer.parseInt(req.getParameter("pageSize")) : 5;
        //获取数据SS
        List<Student> students = studentService.getStudentList(stuName, stuNo, sex, pageIndex, pageSize);
        Integer totalCount = studentService.getStudentTotalCount(stuName, stuNo, sex);
        //数据返回到页面
        //返回总条数
        req.setAttribute("totalCount", totalCount);
        //返回学生列表
        req.setAttribute("students", students);
        //返回页码
        req.setAttribute("pageIndex", pageIndex);
        //返回每页显示条数
        req.setAttribute("pageSize", pageSize);
        //返回总页数
        req.setAttribute("pageTotal", totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1);
        //返回模糊查询的值
        req.setAttribute("stuName", stuName);
        req.setAttribute("stuNo", stuNo);
        req.setAttribute("sex", sex);
        req.getRequestDispatcher("/Educational/student/list.jsp").forward(req, resp);
    }
}
